package com.jcark.utilscode;

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import com.blankj.utilcode.util.Utils

/**
 * Description:Toast工具类 <br>
 * @author King.Zi<br>
 * @date 2022/6/2<br>
 * Email: 78688689@qq.com
 */
object MToastUtils {
    //默认显示
    private var isShow = true

    //全局唯一的Toast
    private val mToast: Toast? = null

    /**
     * 全局控制是否显示Toast
     *
     * @param isShowToast
     */
    fun controlShow(isShowToast: Boolean) {
        isShow = isShowToast
    }

    /**
     * 取消Toast显示
     */
    fun cancelToast() {
        if (isShow && mToast != null) {
            mToast.cancel()
        }
    }

    fun shortShow(message: CharSequence) {
        show(Utils.getApp(), message, true, isBottom = false)
    }

    fun shortShow(@StringRes resId: Int) {
        show(Utils.getApp(), Utils.getApp().resources.getString(resId), true, isBottom = false)
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    fun shortShow(context: Context = Utils.getApp(), message: CharSequence) {
        show(context, message, true, isBottom = false)
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param resId   资源ID:getResources().getString(R.string.xxxxxx);
     */
    fun shortShow(context: Context = Utils.getApp(), @StringRes resId: Int) {
        shortShow(context, context.resources.getString(resId))
    }

    /**
     * 短时间显示Toast,底部
     *
     * @param context
     * @param message
     */
    fun shortShowAtBottom(context: Context = Utils.getApp(), message: CharSequence) {
        show(context, message, true, isBottom = true)
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param resId   资源ID:getResources().getString(R.string.xxxxxx);
     */
    fun shortShowAtBottom(context: Context = Utils.getApp(), @StringRes resId: Int) {
        shortShowAtBottom(context, context.resources.getString(resId))
    }

    /**
     * 长时间显示
     *
     * @param context
     * @param message
     */
    fun longShow(context: Context = Utils.getApp(), message: CharSequence) {
        show(context, message, false, isBottom = false)
    }

    /**
     * 长时间显示
     *
     * @param context
     * @param resId   资源ID:getResources().getString(R.string.xxxxxx);
     */
    fun longShow(context: Context = Utils.getApp(), @StringRes resId: Int) {
        shortShow(context, context.resources.getString(resId))
    }

    /**
     * 显示提示信息
     */
    private fun show(
        context: Context = Utils.getApp(),
        message: CharSequence,
        isShowToast: Boolean,
        isBottom: Boolean
    ) {
        if (TextUtils.isEmpty(message) || context == null) {
            return
        }
        // 网络请求onFailure中应打印log和修改页面UI，但项目中弹出的Toast，只能在这做拦截
        if (message.toString().contains("Exception") || message.toString()
                .contains("Unable to resolve host") || message.toString().contains("Attempt to invoke")
        ) {
            return
        }
        if (isShow) {
            //使用布局加载器，将编写的toast_layout布局加载进来
            val view: View = LayoutInflater.from(context).inflate(R.layout.layout_toast, null)
            //获取TextView
            val title = view.findViewById<View>(R.id.toast_tv) as TextView
            //设置显示的内容
            title.text = message
            val toast = Toast(context)
            if (isBottom) {
                toast.setGravity(Gravity.BOTTOM, 0, 300)
            } else {
                toast.setGravity(Gravity.CENTER, 0, 0)
            }
            //设置显示时间
            toast.duration = if (isShowToast) Toast.LENGTH_SHORT else Toast.LENGTH_LONG
            toast.view = view
            toast.show()
        }
    }
}